﻿// CheckProjectBrowserDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "CheckProjectBrowserDialog.h"
#include "afxdialogex.h"


// CCheckProjectBrowserDialog 对话框

IMPLEMENT_DYNAMIC(CCheckProjectBrowserDialog, CDialogEx)

CCheckProjectBrowserDialog::CCheckProjectBrowserDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CHECK_PROJECT_BROWSER, pParent)
{
	m_nProjCode = -1;
}

CCheckProjectBrowserDialog::~CCheckProjectBrowserDialog()
{
}

void CCheckProjectBrowserDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MFCPROPERTYGRID2, m_props);
	DDX_Control(pDX, IDC_LIST_PROJECT, m_list);
}


BEGIN_MESSAGE_MAP(CCheckProjectBrowserDialog, CDialogEx)
	ON_WM_SIZE()
	ON_NOTIFY(NM_CLICK, IDC_LIST_PROJECT, &CCheckProjectBrowserDialog::OnNMClickListProject)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_PROJECT, &CCheckProjectBrowserDialog::OnNMDblclkListProject)
END_MESSAGE_MAP()


// CCheckProjectBrowserDialog 消息处理程序


BOOL CCheckProjectBrowserDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_list.AttachBitmap();
	m_list.Populate();

	if (m_list.GetItemCount() > 0)
	{
		PopulateProjectProperty(0);
	}

	AdjustLayout();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CCheckProjectBrowserDialog::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);
	
	AdjustLayout();
	
	// TODO: 在此处添加消息处理程序代码
}

void CCheckProjectBrowserDialog::AdjustLayout()
{
	CWnd* pWnd = GetDlgItem(IDC_STATIC_PROPS);
	if (pWnd)
	{
		CRect rect;
		GetDlgItem(IDC_STATIC_PROPS)->GetWindowRect(rect);
		ScreenToClient(rect);
		int margin = 2;
		rect.left += margin;
		rect.right -= margin;
		rect.top += margin;
		rect.bottom -= margin;
		m_props.MoveWindow(rect);
	}
}

void CCheckProjectBrowserDialog::OnNMClickListProject(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	int nItem = pNMItemActivate->iItem;
	if (nItem < 0)
	{
		return;
	}
	PopulateProjectProperty(nItem);
	
	*pResult = 0;
}

void CCheckProjectBrowserDialog::PopulateProjectProperty(int nProjItem)
{
	m_nProjCode = m_list.GetItemData(nProjItem);

	ck::CheckProject* pProject = NULL;
	ck::CheckProjectManager* pProjectManager = ck::CheckProjectManager::GetInstance();
	pProject = pProjectManager->GetProject(m_nProjCode);
	if (pProject)
	{
		m_props.Populate(pProject);

		pProject->Release();
	}
}

z_int32 CCheckProjectBrowserDialog::GetSelectedProjectCode()
{
	return m_nProjCode;
}

void CCheckProjectBrowserDialog::OnNMDblclkListProject(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}
